package com.xms.dependency.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.core.exception.XmsException;
import com.xms.core.module.ModuleCollection;
import com.xms.datacore.service.CreaterService;
import com.xms.dependency.entity.Dependency;
import com.xms.utils.CollectionUtil;
import com.xms.utils.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.sql.SQLException;
import java.util.*;

/**
 * <p>
 * DependencyCreaterService
 * </p>
 *
 * @author migo
 * @since 2021/10/23
 */
@Service
public class DependencyCreaterService extends CreaterService<Dependency> implements IDependencyCreaterService {

    @Override
    public boolean create(Dependency entity) throws XmsException, SQLException {
        if (super.getService().count(new LambdaQueryWrapper<Dependency>()
                .eq(Dependency::getDependentcomponenttype, entity.getDependentcomponenttype())
                .eq(Dependency::getDependentobjectid, entity.getDependentobjectid())
                .eq(Dependency::getRequiredcomponenttype, entity.getRequiredcomponenttype())
                .eq(Dependency::getRequiredobjectid, entity.getRequiredobjectid())) > 0)
        {
            return false;
        }
        return super.create(entity);
    }

    @Override
    public boolean create(int dependentComponentType, UUID dependentObjectId, int requiredComponentType, UUID requiredObjectId) throws XmsException, SQLException {
        return this.create(dependentComponentType, dependentObjectId, requiredComponentType, Arrays.asList(requiredObjectId));
    }

    @Override
    public boolean create(String dependentComponentName, UUID dependentObjectId, String requiredComponentName, UUID requiredObjectId) throws XmsException, SQLException {
        return this.create(dependentComponentName, dependentObjectId, requiredComponentName, Arrays.asList(requiredObjectId));
    }

    @Override
    public boolean create(int dependentComponentType, UUID dependentObjectId, int requiredComponentType, Collection<UUID> requiredObjectId) throws XmsException, SQLException {
        Assert.notEmpty(requiredObjectId, "requiredObjectId");
        List<Dependency> existEntities = super.getService().list(new LambdaQueryWrapper<Dependency>()
        .eq(Dependency::getDependentcomponenttype, dependentComponentType)
        .eq(Dependency::getDependentobjectid, dependentObjectId)
        .eq(Dependency::getRequiredcomponenttype, requiredComponentType)
        .in(Dependency::getRequiredobjectid, requiredObjectId));
        List<Dependency> entities = new ArrayList<>();
        for (UUID item : requiredObjectId)
        {
            if (UUIDUtil.isEmpty(item))
            {
                continue;
            }
            if (existEntities != null
                    && existEntities.stream().anyMatch(x -> x.getDependentcomponenttype() == dependentComponentType && x.getDependentobjectid().equals(dependentObjectId)
                    && x.getRequiredcomponenttype() == requiredComponentType && x.getRequiredobjectid().equals(item)))
            {
                continue;
            }
            Dependency entity = new Dependency();
            entity.setDependencyid(UUIDUtil.get());
            entity.setDependentcomponenttype(dependentComponentType);
            entity.setDependentobjectid(dependentObjectId);
            entity.setRequiredcomponenttype(requiredComponentType);
            entity.setRequiredobjectid(item);
            entities.add(entity);
        }
        if (CollectionUtil.notEmpty(entities))
        {
            return super.createMany(entities);
        }
        return false;
    }

    @Override
    public boolean create(String dependentComponentName, UUID dependentObjectId, String requiredComponentName, Collection<UUID> requiredObjectId) throws XmsException, SQLException {
        return this.create(ModuleCollection.getIdentity(dependentComponentName), dependentObjectId, ModuleCollection.getIdentity(requiredComponentName), requiredObjectId);
    }
}
